{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "079dc387",
   "metadata": {},
   "source": [
    "----------------------\n",
    "* 2022-11-21 week14\n",
    "* Python 学习记录\n",
    "* 记录人：刘志勇\n",
    "----------------------\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fd36c00",
   "metadata": {},
   "source": [
    "# 本周目标\n",
    "> 1、函数的定义\n",
    "\n",
    "> 2、如何使用函数\n",
    "\n",
    "> 3、函数的返回值\n",
    "\n",
    "> 4、使用注释改进文档\n",
    "\n",
    "> 5、传递任意数量的实参\n",
    "\n",
    "> 6、函数与模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dc95863a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello \"+username+\"~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "69a8cc85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello 汤姆~\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"汤姆\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ead954b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    \"\"\"y=2x的函数计算\"\"\"\n",
    "    print(2*x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3eb260b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "f(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9dcf5c8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践 编程思维（计算思维）下，函数的应用\n",
    "def nfu_news_url(类别,page):\n",
    "    for page in range (page):\n",
    "        print(\"https://www.nfu.edu.cn/\" + 类别 + \"/index\" + str(page) + \".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "9dbedc60",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/xxyw/index0.htm\n",
      "https://www.nfu.edu.cn/xxyw/index1.htm\n",
      "https://www.nfu.edu.cn/xxyw/index2.htm\n",
      "https://www.nfu.edu.cn/xxyw/index3.htm\n",
      "https://www.nfu.edu.cn/xxyw/index4.htm\n",
      "https://www.nfu.edu.cn/xxyw/index5.htm\n",
      "https://www.nfu.edu.cn/xxyw/index6.htm\n",
      "https://www.nfu.edu.cn/xxyw/index7.htm\n",
      "https://www.nfu.edu.cn/xxyw/index8.htm\n",
      "https://www.nfu.edu.cn/xxyw/index9.htm\n"
     ]
    }
   ],
   "source": [
    "nfu_news_url(\"xxyw\",10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9b0a4e2",
   "metadata": {},
   "source": [
    "# 2.如何使用函数（传递实参）\n",
    "\n",
    "> 1.positional aguement(位置参数)\n",
    "\n",
    "> 2.keyword aguement(关键字参数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "eb6cf1d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def f(x,y):\n",
    "     print(x)\n",
    "     print(y)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b079dc98",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (3059410298.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [25]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(x=3,1)\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(x=3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "94a29841",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "abf56aef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "41c76f88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c6fcfdd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b8162b7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "f(3,2,5) # 根据上面的经验，这个应该是位置参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7b9e4185",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "f(y=10,x=3,z=7) # 这个应该是关键字参数，关键字参数不考虑顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "47268249",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "f(3,z=10,y=9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "09dc7029",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "f(3,y=10,z=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "9b7b7802",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (1857774082.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [37]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(3,y=9,10)\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(3,y=9,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8a9fcf3",
   "metadata": {},
   "source": [
    "## 3.默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1b6af9dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "Myhamster's name is Harry.\n",
      "\n",
      "I have a cat.\n",
      "Mycat's name is Python.\n",
      "\n",
      "I have a dog.\n",
      "Mydog's name is Jerry.\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(pet_name,animal_type=\"dog\",):\n",
    "    \"\"\"显示宠物信息，如果你的宠物是dog，可以不选择填写animal_type\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    "\n",
    "describe_pet('harry','hamster')\n",
    "describe_pet('python','cat')\n",
    "describe_pet('Jerry')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ba61a508",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world 你好 世界hello world 你好 世界\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\",\"world\",\"你好\",\"世界\",end=\"\")\n",
    "print(\"hello\",\"world\",\"你好\",\"世界\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4b1a2d6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "9d0d1800",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function describe_pet in module __main__:\n",
      "\n",
      "describe_pet(pet_name, animal_type='dog')\n",
      "    显示宠物信息，如果你的宠物是dog，可以不选择填写animal_type\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(describe_pet)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86837ba5",
   "metadata": {},
   "source": [
    "## 3. 函数的返回值\n",
    "> 1.几乎所有编程语言，函数是要求必须要写返回值！\n",
    "\n",
    "> 2.Python作为高级语言，可以不写返回值，但是一般情况下，也是要写的，不写的话，默认返回None\n",
    "\n",
    "> 3. 函数的返回值：return\n",
    ">> 3.1 return 可以返回过程运算的结果\n",
    ">> 3.2 return 可以返回过程自定义的结果（由程序开发人员/设计人员定制的）\n",
    "## 函数的优势\n",
    "* 1. 可重复使用\n",
    "* 2. 具有强大的可扩展性\n",
    "* 3. 具有可定制化性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bce409e5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "94270a9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y=x+2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "7c5b436f",
   "metadata": {},
   "outputs": [],
   "source": [
    " result = f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "b5a7cdf6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "1f79b243",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y=x+2\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "94c55567",
   "metadata": {},
   "outputs": [],
   "source": [
    " result = f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "2288927d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a090e39f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "629c07e1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
